<%-- 
    Document   : Admin1
    Created on : Feb 29, 2012, 3:56:37 PM
    Author     : Y.Yang
--%>

<%@page import = "java.sql.*, java.util.*,  javax.mail.*, javax.mail.internet.*, javax.activation.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <title>Admin1</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>

        <%
         String url = "jdbc:mysql://localhost:3306/ismi";
         Connection con;
         
//         ResultSet rs;
         String[] c=request.getParameterValues("c1");
         for(int i = 0; i<c.length; i++){
             
             c[i] = c[i].replace('\n',' ');
             c[i] = c[i].trim();
             System.out.print(c[i]);
             
         }
         
         
         try 
         {
              Class.forName("com.mysql.jdbc.Driver");
         } 
         catch(java.lang.ClassNotFoundException e) 
         {                       
              System.err.print("ClassNotFoundException: ");
              System.err.println(e.getMessage());
         }

         try  
         {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url,"root", "isaac16");
            System.out.println("test");
            //Statement st = con.createStatement();
                       //Statement stDelete = con.createStatement();
                       //for(String s:c)
            PreparedStatement preparedStatement = null;              
            for(String s:c){
           // String sqlstrdel = "DELETE from user where user_name=?";
            PreparedStatement delUser = con.prepareStatement( "select email from user where user_name = ?");
            delUser.setString(1, s);
            System.out.println(s);
            ResultSet resultSet = delUser.executeQuery();
            System.out.println(resultSet);
            String to = "";
            
                
                
            preparedStatement = con.prepareStatement("DELETE from user where user_name=?");
	    preparedStatement.setString(1,s);
            preparedStatement.executeUpdate();
            out.println("Sucess");
            
            while (resultSet.next())
           {
               to = resultSet.getString(1);
              
           }
            System.out.println(to);
            String host = "mail.albany.edu";
            
            String from = "yyang7@albany.edu";
            String subject = "I am sorry, you are deleted from Semetech";
            String messageText = "I am very sorry, according to you activity, I have to delete you from our community.\nBut you can registrate again.\nThank you!";

            boolean sessionDebug = false;

            Properties props = System.getProperties();
            props.put("mail.host", host);
            props.put("mail.transport.protocol", "smtp");

            Session mailSession = Session.getDefaultInstance(props, null);
            mailSession.setDebug(sessionDebug);

            
            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress(from)); 
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject);
            msg.setSentDate(new java.util.Date());
            msg.setText(messageText);
            
            Transport.send(msg);
            
            System.out.println(c[0]);
            }
            response.sendRedirect("AdminPage.jsp");
            //}
                       
                       
                       
            }
            catch(SQLException ex) 
               {
                       System.err.println("SQLException: " + ex.getMessage());
               }
        
        %>
    </body>
</html>